﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using uTools;
public class Level5ButtonHeart : MonoBehaviour
{
    public GameLevelItem gameLevelItem;
    public uTools.uTweenAlpha tweenAlpha;
    public uTools.uTweenAlpha tweenAlpha2;
    public List<Level5ButtonHeartButton> btnList = new List<Level5ButtonHeartButton>();
    public List<int> indexList = new List<int>();
    private int curIndex = 0;
    private bool isFinished = false;
    public Image heartImage;

    private void Start()
    {
        int count = btnList.Count;
        for (int i = 0; i < count; i++)
        {
            btnList[i].Init(i, this);
        }
    }

    public void BtnEvent(Level5ButtonHeartButton btn)
    {
        if (isFinished) return;
        if (indexList[curIndex] == btn.btnIndex)
        {
            if (curIndex == 0)
                btn.ActiveBtn(true);
            curIndex++;
            int nextBtnIndex = indexList[curIndex];
            btnList[nextBtnIndex].ActiveBtn(true);
            CheckResult();
        }
        else
        {
            HideAllBtn();
        }
    }

    private void HideAllBtn()
    {
        int count = btnList.Count;
        for (int i = 0; i < count; i++)
        {
            btnList[i].ActiveBtn(false);
        }
        curIndex = 0;
    }

    private void CheckResult()
    {
        if (curIndex >= indexList.Count - 1)
        {
            isFinished = true;
            tweenAlpha.enabled = true;
            Invoke("TriggerResult", 1.0f);
            tweenAlpha2.enabled = true;
        }
    }

    private void TriggerResult()
    {
        gameLevelItem.TriggerResult(true);
    }

}
